package cn.iocoder.devlop.module.annual.report.wsbaseinfo.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.wsbaseinfo.controller.vo.*;
import cn.iocoder.devlop.module.annual.report.wsbaseinfo.dal.WsBaseinfoMapper;
import cn.iocoder.devlop.module.annual.report.wsbaseinfo.dal.dataobject.WsBaseinfoDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 外商投资基本信息 Service 实现类
 *
 * @author sunhao
 */
@Service
@Validated
public class WsBaseinfoServiceImpl implements WsBaseinfoService {

    @Resource
    private WsBaseinfoMapper wsBaseinfoMapper;

    @Resource
    private MarpripinfoService marpripinfoService;


    @Override
    public void updateBaseinfo(WsBaseinfoUpdateVO updateReqVO) {
        if(!validateWsBaseinfoExists(updateReqVO.getRegno(),updateReqVO.getAncheyear())){
            initWsBaseinfo(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        }
        WsBaseinfoDO wsBaseinfoDO = BeanUtil.toBean(updateReqVO, WsBaseinfoDO.class);
        wsBaseinfoDO.setAncheid(IdGenerateUtil.getAncheid(updateReqVO.getRegno(),updateReqVO.getAncheyear()));
        wsBaseinfoMapper.updateById(wsBaseinfoDO);
    }

    @Override
    public void updateAssetsLiabilitiesinfo(WsAssetsLiabilitiesinfoVO updateReqVO) {
        if(!validateWsBaseinfoExists(updateReqVO.getRegno(),updateReqVO.getAncheyear())){
            initWsBaseinfo(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        }
        WsBaseinfoDO wsBaseinfoDO = BeanUtil.toBean(updateReqVO, WsBaseinfoDO.class);
        wsBaseinfoDO.setAncheid(IdGenerateUtil.getAncheid(updateReqVO.getRegno(),updateReqVO.getAncheyear()));
        wsBaseinfoMapper.updateById(wsBaseinfoDO);
    }

    @Override
    public void updateDebtinfo(WsDebtinfoUpdateVO updateReqVO) {
        if(!validateWsBaseinfoExists(updateReqVO.getRegno(),updateReqVO.getAncheyear())){
            initWsBaseinfo(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        }
        WsBaseinfoDO wsBaseinfoDO = BeanUtil.toBean(updateReqVO, WsBaseinfoDO.class);
        wsBaseinfoDO.setAncheid(IdGenerateUtil.getAncheid(updateReqVO.getRegno(),updateReqVO.getAncheyear()));
        wsBaseinfoMapper.updateById(wsBaseinfoDO);
    }

    @Override
    public void updateDutyFreeinfo(WsDutyFreeinfoUpdateVO updateReqVO) {
        if(!validateWsBaseinfoExists(updateReqVO.getRegno(),updateReqVO.getAncheyear())){
            initWsBaseinfo(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        }
        WsBaseinfoDO wsBaseinfoDO = BeanUtil.toBean(updateReqVO, WsBaseinfoDO.class);
        wsBaseinfoDO.setAncheid(IdGenerateUtil.getAncheid(updateReqVO.getRegno(),updateReqVO.getAncheyear()));
        wsBaseinfoMapper.updateById(wsBaseinfoDO);
    }

    @Override
    public void updateOperateinfo(WsOperateinfoUpdateVO updateReqVO) {
        if(!validateWsBaseinfoExists(updateReqVO.getRegno(),updateReqVO.getAncheyear())){
            initWsBaseinfo(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        }
        WsBaseinfoDO wsBaseinfoDO = BeanUtil.toBean(updateReqVO, WsBaseinfoDO.class);
        wsBaseinfoDO.setAncheid(IdGenerateUtil.getAncheid(updateReqVO.getRegno(),updateReqVO.getAncheyear()));
        wsBaseinfoMapper.updateById(wsBaseinfoDO);
    }

    @Override
    public WsBaseinfoUpdateVO getWsBaseinfo(String regno, String ancheyear) {
        WsBaseinfoDO wsBaseinfoDO = getWsBaseinfoAll(IdGenerateUtil.getAncheid(regno,ancheyear));
        return BeanUtil.toBean(wsBaseinfoDO, WsBaseinfoUpdateVO.class);
    }

    @Override
    public WsAssetsLiabilitiesinfoVO getAssetsLiabilitiesinfo(String regno, String ancheyear) {
        WsBaseinfoDO wsBaseinfoDO = getWsBaseinfoAll(IdGenerateUtil.getAncheid(regno,ancheyear));
        return BeanUtil.toBean(wsBaseinfoDO, WsAssetsLiabilitiesinfoVO.class);
    }

    @Override
    public WsDebtinfoUpdateVO getDebtinfo(String regno, String ancheyear) {
        WsBaseinfoDO wsBaseinfoDO = getWsBaseinfoAll(IdGenerateUtil.getAncheid(regno,ancheyear));
        return BeanUtil.toBean(wsBaseinfoDO, WsDebtinfoUpdateVO.class);
    }

    @Override
    public WsDutyFreeinfoUpdateVO getDutyFreeinfo(String regno, String ancheyear) {
        WsBaseinfoDO wsBaseinfoDO = getWsBaseinfoAll(IdGenerateUtil.getAncheid(regno,ancheyear));
        return BeanUtil.toBean(wsBaseinfoDO, WsDutyFreeinfoUpdateVO.class);
    }

    @Override
    public WsOperateinfoUpdateVO getOperateinfo(String regno, String ancheyear) {
        WsBaseinfoDO wsBaseinfoDO = getWsBaseinfoAll(IdGenerateUtil.getAncheid(regno,ancheyear));
        return BeanUtil.toBean(wsBaseinfoDO, WsOperateinfoUpdateVO.class);
    }

    @Override
    public void updateBaseinfoAll(WsBaseinfoDO updateReqVO) {
        WsBaseinfoDO wsBaseinfoDO = getWsBaseinfoAll(updateReqVO.getAncheid());
        if (ObjUtil.isNotNull(wsBaseinfoDO)){
            wsBaseinfoMapper.updateById(updateReqVO);
        }else {
            wsBaseinfoMapper.insert(updateReqVO);
        }
    }

    @Override
    public WsBaseinfoDO getWsBaseinfoAll(String ancheid) {
        LambdaQueryWrapperX<WsBaseinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(WsBaseinfoDO::getAncheid,ancheid);
        return wsBaseinfoMapper.selectOne(wrapperX);
    }

    private Boolean validateWsBaseinfoExists(String regno, String ancheyear){
        return ObjUtil.isNotNull(getWsBaseinfoAll(IdGenerateUtil.getAncheid(regno,ancheyear)));
    }

    private void initWsBaseinfo(String regno, String ancheyear){
        MarpripinfoDO marpripinfo = marpripinfoService.getMarpripinfo(regno);
        WsBaseinfoDO wsBaseinfoDO = new WsBaseinfoDO();
        wsBaseinfoDO.setAncheid(IdGenerateUtil.getAncheid(regno,ancheyear));
        wsBaseinfoDO.setPripid(marpripinfo.getId());
        wsBaseinfoDO.setAnchedate(LocalDateTime.now());
        wsBaseinfoDO.setAncheyear(ancheyear);
        wsBaseinfoDO.setUniscid(marpripinfo.getUniscid());
        wsBaseinfoDO.setEnentname(marpripinfo.getEntname());
    }
}
